import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:xianyu_school/utils/url.dart';
import 'package:xianyu_school/xianyuApp/entity/goods/goods.dart';
import 'package:xianyu_school/xianyuApp/home/List/homeGoodItem.dart';
import 'package:xianyu_school/xianyuApp/mine/Widgets/BallPulseIndicator.dart';

class searchDetailPage extends StatefulWidget{
  searchDetailPage({Key key,this.t}) : super(key: key);
  final String t;
  @override
  State<StatefulWidget> createState() {
    return _searchDetailPageState();
  }
}

class _searchDetailPageState extends State<searchDetailPage>{

  String url = urls().url;
  List<goods> gs = [];
  Future searchgoods() async {
    try{
      Response re = await Dio().get(url+"/goods/searchgoods",queryParameters: {"target":widget.t});
      return re;
    }
    catch(e){
      return ;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CupertinoNavigationBar(
        backgroundColor: Colors.yellow,
        middle: const Text(''),
      ),
      body: FutureBuilder(
          future: searchgoods(),
          builder: (context, snapshot){
            if(snapshot.hasData){
              if (snapshot.data.toString() == "No more data"){
                return Text(snapshot.data.toString());
              }
              else{
                var responseJson = json.decode(snapshot.data.toString());
                responseJson.forEach((data) {
                  gs.add(goods.fromJson(data));
                });
                return CustomScrollView(
                  slivers: <Widget>[
                    // 顶部偏移量
//                    SliverOverlapInjector(
//                      handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
//                    ),

                    // 商品列表
                    SliverPadding(
                      padding: EdgeInsets.fromLTRB(10, 5, 10, 10),
                      sliver: SliverStaggeredGrid.countBuilder(

                        crossAxisCount: 1,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10,
                        itemCount: gs.length,
                        itemBuilder: (BuildContext context, int index) {
                          return FHHomeGoodItem(model: gs[index],);
                        },
                        staggeredTileBuilder: (int index) {
                          return StaggeredTile.fit(2);
                        },
                      ),
                    ),

                    // 刷新控件
                  ],
                );
              }
            }
            else return BallPulseIndicator(
                color: Colors.black
            );
          }),
    );
  }
}